package top.q3747051.cq;


import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;


import java.io.IOException;


public class CQPlugin extends JavaPlugin implements Listener {


    private CQSocketManager man;

    @Override
    public void onEnable() {
        getLogger().info("QCPlugin Start");
        Bukkit.getPluginManager().registerEvents(this,this);

        try {
            man = new CQSocketManager(this,35465,s ->
                this.getServer().getOnlinePlayers().forEach(p -> p.sendMessage(s))
            );
            man.listen();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void onDisable() {
        getLogger().info("QCPlugin Stop");
        man.close();
    }

    @EventHandler
    public void onPlayerSay(AsyncPlayerChatEvent event){
        String msg = "["+event.getPlayer().getName() + "]>>>"+event.getMessage();
        getLogger().info(msg);
        man.send(msg);
    }
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event){
        String msg = event.getPlayer().getName() +"离开了游戏!";
        getLogger().info(msg);
        man.send(msg);
    }
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
        String msg = event.getPlayer().getName() + "上线了!";
        getLogger().info(msg);
        man.send(msg);
    }

}